package com.sevenGroup.pawnshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevenGroup.pawnshop.Utils.CopyProperties;
import com.sevenGroup.pawnshop.controller.form.UserForm;
import com.sevenGroup.pawnshop.dao.RolesDao;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.Roles;
import com.sevenGroup.pawnshop.model.Users;
import com.sevenGroup.pawnshop.dao.UsersDao;
import com.sevenGroup.pawnshop.service.UsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersDao, Users> implements UsersService {
    @Resource
    private UsersDao usersDao;

    @Resource
    private RolesDao rolesDao;
//        通过账号ID查询用户对象
    @Override
    public Users getUserByaid(Integer aid) {
        Users users = usersDao.selectOne(new QueryWrapper<Users>().eq("account_id", aid));
        return users;
    }

    @Override
    public void changeRole(UserForm userForm) throws sevenGroupException {
        Roles roles = rolesDao.selectById(userForm.getRoleId());
        if(roles==null){
            throw new sevenGroupException(40021,"没有该角色");
        }
        userForm.setRoleName(roles.getRoleName());
        userForm.setRoleManage(roles.getRoleManage());
        usersDao.updateById(CopyProperties.copyOne(userForm,Users.class));
    }
}
